package com.blustreaksw.cyclecoach.history;

import java.util.UUID;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.blustreaksw.cyclecoach.R;


/* WorkoutLogEntryFragment
 * 
 * Creates for display the workout log entry view that was selected by the user
 * from WorkoutHistoryListFragment.
 * 
 */
public class WorkoutLogEntryFragment extends Fragment {
	public static final String EXTRA_WORKOUT_ENTRY_ID = "com.blustreaksw.cyclecoach.WORKOUT_ENTRY_ID";
	
	WorkoutLogEntry mLogEntry;
	
    public static WorkoutLogEntryFragment newInstance(UUID logEntryId) {
        Bundle args = new Bundle();
        args.putSerializable(EXTRA_WORKOUT_ENTRY_ID, logEntryId);

        WorkoutLogEntryFragment fragment = new WorkoutLogEntryFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        UUID logEntryId = (UUID)getArguments().getSerializable(EXTRA_WORKOUT_ENTRY_ID);
        mLogEntry = WorkoutHistoryContainer.get(getActivity()).getLogEntry(logEntryId);
        
        getActivity().setTitle(R.string.subactivity_workoutLogEntry_title);
        setHasOptionsMenu(true);
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_workout_log_entry, parent, false);
        mLogEntry.setTitle(getString(R.string.workoutLogEntry_title));
        
        TextView resultCountView = (TextView)v.findViewById(R.id.resultCountView);
        resultCountView.setText("Entry " + Integer.toString(mLogEntry.getLogEntryNumber()) 
        		+ " of " + Integer.toString(mLogEntry.getLogEntryCount()));
        
        getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
        return v;
    }
    
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    	super.onCreateOptionsMenu(menu, inflater);
    	inflater.inflate(R.menu.fragment_workout_log_entry, menu);
    }
    
    @Override
	public boolean onOptionsItemSelected(MenuItem item) {
    	switch(item.getItemId()) {
    		case android.R.id.home:
    			if(NavUtils.getParentActivityName(getActivity()) != null) {
    				NavUtils.navigateUpFromSameTask(getActivity());
    			}
    			return true;
    		
    		default:
    			return super.onOptionsItemSelected(item); 
    	}
    }
	
}
